﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DAL;
using HomeStayWebCP.Models.BusinessObject;
using HomeStayWebCP.Models;
using HomeStayWebCP.Utility;
namespace HomeStayWebCP.Models
{
    public class PostModel
    {
        public List<WishListItem> getWishListOfUser(string username)
        {
            PostsDataModel dalModel = new PostsDataModel();
            List<WishListItem> listResult = new List<WishListItem>();
            List<Store_Post_ViewWishListOfUser_Result> list = dalModel.getWishListOfUser(username);
            foreach (var i in list)
            {
                WishListItem witem = new WishListItem();
                witem.ImageName = Util.imagePath + i.PostAvatar;
                witem.PostStatus = i.PostStatus;
                witem.PostID = i.PostID;
                witem.PostTitle = i.PosTitle;
                witem.PricePerDay = Double.Parse(i.PricePerDay.ToString());
                witem.PricePerWeek = Double.Parse(i.PricePerWeek.ToString());
                witem.PricePerMonth = Double.Parse(i.PricePerMonth.ToString());
                witem.Address = i.Privince + ", " + i.District;
                witem.TimeAdded = i.TimeCreated.ToShortDateString();
                listResult.Add(witem);
            }
            return listResult;
        }
        public bool deleteWishListOfUser(int postID,string username)
        {
            PostsDataModel dalModel = new PostsDataModel();
            bool result = dalModel.deleteWishListOfUser(postID, username);
            return result;
        }
        public List<string> getHomestayname(int postID)
        {
            PostsDataModel dalModel = new PostsDataModel();
            List<Store_HomeStayType_GetNameHomeStayType_Result> l= dalModel.getHomestayname(postID);
            List<string> result = new List<string>();
            foreach (var item in l)
            {
                result.Add(item.HomeStayTypeName);
            }
            return result;
        }
        public PostItem GetDetailOfPost(int postID)
        {
            PostsDataModel model = new PostsDataModel();
            Store_Post_GetDetailOfPost_Result detailPost = model.GetDetailOfPost(postID);
            RegionModel remodel = new RegionModel();

            if (detailPost!=null)
            {
                Store_Post_GetDetailOfPost_Result temp = detailPost;
                PostItem pitem = new PostItem();
                pitem.AddressDetail = temp.AddressDetail;
                pitem.Area = temp.Area;
                pitem.ContactName = temp.ContactName;
                pitem.Content = temp.Content != null ? temp.Content.Replace(System.Environment.NewLine, "<br />") : "";
                pitem.District = remodel.SelectNameFromID(temp.District);
                pitem.Email = temp.ContactEmail;
                pitem.HasGara = temp.HasGara;
                pitem.HasGarden = temp.HasGarden;
                pitem.HasKitchen = temp.HasKitchen;
                pitem.MobilePhone = temp.MobilePhone;
                pitem.NumBadroom = temp.NumBadroom;
                pitem.NumOfWC = temp.NumOfWC;
                pitem.NumSittingRoom = temp.NumSittingRoom;
                pitem.Ordinate = temp.Ordinate;
                pitem.PostAvailable = temp.IsAvailable;
                pitem.PostID = temp.PostID;
                pitem.PricePerDay = temp.PricePerDay;
                pitem.PricePerMonth = temp.PricePerMonth;
                pitem.PricePerWeek = temp.PricePerWeek;
                pitem.Province = remodel.SelectNameFromID(temp.Province);
                pitem.Storey = temp.Storey;
                pitem.TelePhone = temp.TelePhone;
                pitem.TimeCreated = temp.TimeCreated;
                pitem.Title = temp.Title;
                pitem.TypeOfHouse = temp.TypeOfHouse;
                pitem.ProvinceID = temp.Province;
                pitem.DistrictID = temp.District;
                pitem.Status = temp.PostStatus;
                return pitem;
            }
            else
            {
                return null;
            }

        }

        #region Hoangdx - Code Posts Function

        DAL.PostsDataModel model = new DAL.PostsDataModel();

        #region New Post
        public List<DAL.Post_ListHomeStayTypes_View> GetHomeStayTypes()
        {
            return model.GetListHomeStayTypes(); ;
        }

        public void CreateNewPost(PostObject postDTO, List<string> ListImageFiles, string[] arrChecked)
        {
            //Map PostObject to PostEntityObject(DAL)
            double priceperday = Double.Parse((postDTO.House.PricePerDay).ToString());
            double priceperweek = Double.Parse((postDTO.House.PricePerWeek).ToString());
            double pricepermonth = Double.Parse((postDTO.House.PricePerMonth).ToString());
            DAL.Post postDB = new DAL.Post()
            {
                Title = postDTO.Title,
                Content = postDTO.Content.Replace("\n", System.Environment.NewLine),
                ContactName = postDTO.ContactName,
                MobilePhone = postDTO.MobilePhone,
                TelePhone = postDTO.TelePhone,
                ContactEmail = postDTO.Email,
                IsAvailable = true,
                TimeCreated = DateTime.Now,
                PostStatus = 0,
                Username = postDTO.Username
            };
            DAL.House houseDB = new DAL.House()
            {

                TypeOfHouseID =postDTO.House.TypeOfHouseID,
                Area = postDTO.House.Area,
                Province = postDTO.House.ProvinceID,
                District = postDTO.House.DistrictID,
                AddressDetail = postDTO.House.AddressDetail,
                //PricePerDay = postDTO.House.PricePerDay,
                //PricePerWeek = postDTO.House.PricePerWeek,
                //PricePerMonth = postDTO.House.PricePerMonth,
                NumSittingRoom = postDTO.House.NumSittingRoom,
                NumBadroom = postDTO.House.NumBadroom,
                Storey = postDTO.House.Storey,
                NumOfWC = postDTO.House.NumOfWC,
                HasGara = postDTO.House.HasGara,
                HasGarden = postDTO.House.HasGarden,
                HasKitchen = postDTO.House.HasKitchen,
                Ordinate = postDTO.House.Ordinate
            };
            //Th1:Nhap ngay,Khong nhap tuan,khong nhap thang
            if(priceperday != 0 && priceperweek == 0 && pricepermonth == 0)
            {
                houseDB.PricePerDay = postDTO.House.PricePerDay;
                houseDB.PricePerWeek = postDTO.House.PricePerDay*7;
                houseDB.PricePerMonth = postDTO.House.PricePerDay*30;

            }
            //Th2:Khong nhap ngay,Nhap tuan, ko nhap thang
            if (priceperday == 0 && priceperweek != 0 && pricepermonth == 0)
            {
                houseDB.PricePerDay = postDTO.House.PricePerDay;
                houseDB.PricePerWeek = postDTO.House.PricePerWeek;
                houseDB.PricePerMonth = postDTO.House.PricePerWeek*4;

            }
            //Th3 Ko nhap ngay,ko nhap tuan,nhap thang
            if (priceperday == 0 && priceperweek == 0 && pricepermonth != 0)
            {
                houseDB.PricePerDay = postDTO.House.PricePerDay;
                houseDB.PricePerWeek = postDTO.House.PricePerWeek;
                houseDB.PricePerMonth = postDTO.House.PricePerMonth;

            }
            //Th4 nhap ngay,nhap tuan,ko nhap thang
            if (priceperday != 0 && priceperweek != 0 && pricepermonth == 0)
            {
                houseDB.PricePerDay = postDTO.House.PricePerDay;
                houseDB.PricePerWeek = postDTO.House.PricePerWeek;
                houseDB.PricePerMonth = postDTO.House.PricePerWeek*4;

            }
            //Th5 nhap ngay,ko nhap tuan,nhap thang
            if (priceperday != 0 && priceperweek == 0 && pricepermonth != 0)
            {
                houseDB.PricePerDay = postDTO.House.PricePerDay;
                houseDB.PricePerWeek = postDTO.House.PricePerDay*7;
                houseDB.PricePerMonth = postDTO.House.PricePerMonth;
            }
            //Th6 ko nhap ngay,nhap tuan,nhap thang
            if (priceperday == 0 && priceperweek != 0 && pricepermonth != 0)
            {
                houseDB.PricePerDay = postDTO.House.PricePerDay;
                houseDB.PricePerWeek = postDTO.House.PricePerWeek;
                houseDB.PricePerMonth = postDTO.House.PricePerMonth;
            }
            //Th7 nhap ngay ,nhap tuan ,nhap thang
            if (priceperday != 0 && priceperweek != 0 && pricepermonth != 0)
            {
                houseDB.PricePerDay = postDTO.House.PricePerDay;
                houseDB.PricePerWeek = postDTO.House.PricePerWeek;
                houseDB.PricePerMonth = postDTO.House.PricePerMonth;

            }

            model.CreatePost(postDB, houseDB, ListImageFiles, arrChecked);
        }
        #endregion //New Post

        #region Edit Post

        public PostObject GetEditPost(int postID)
        {
            //Get Edit Post
            Post_GetEditPost_View postObject = model.GetEditPost(postID);

            //Map to PostObject
            HouseObject houseDTO = new HouseObject();
            PostObject postDTO = new PostObject();
            postDTO.Title = postObject.Posts_Title;
            postDTO.Username = postObject.Posts_Username;
            postDTO.Content = postObject.Posts_Content;
            postDTO.ContactName = postObject.Posts_ContactName;
            postDTO.MobilePhone = postObject.Posts_MobilePhone;
            postDTO.TelePhone = postObject.Posts_TelePhone;
            postDTO.Email = postObject.Posts_Email;
            postDTO.Available = postObject.Posts_Available;
            //House
            houseDTO.TypeOfHouseID = postObject.Houses_TypeOfHouseID;
            houseDTO.Area = postObject.Houses_Area;
            houseDTO.ProvinceID = postObject.Houses_Province;
            houseDTO.DistrictID = postObject.Houses_District;
            houseDTO.AddressDetail = postObject.Houses_AddressDetail;
            houseDTO.PricePerDay = postObject.Houses_PricePerDay;
            houseDTO.PricePerWeek = postObject.Houses_PricePerWeek;
            houseDTO.PricePerMonth = postObject.Houses_PricePerMonth;
            houseDTO.NumSittingRoom = postObject.Houses_NumSittingRoom;
            houseDTO.NumBadroom = postObject.Houses_NumBadroom;
            houseDTO.Storey = postObject.Houses_Storey;
            houseDTO.NumOfWC = postObject.Houses_NumOfWC;
            houseDTO.HasGara = postObject.Houses_HasGara;
            houseDTO.HasGarden = postObject.Houses_HasGarden;
            houseDTO.HasKitchen = postObject.Houses_HasKitchen;
            houseDTO.Ordinate = postObject.Houses_Ordinate;
            postDTO.House = houseDTO;

            return postDTO;
        }

        public void UpdatePost(PostObject postDTO)
        {
            double priceperday = Double.Parse((postDTO.House.PricePerDay).ToString());
            double priceperweek = Double.Parse((postDTO.House.PricePerWeek).ToString());
            double pricepermonth = Double.Parse((postDTO.House.PricePerMonth).ToString());
            DAL.Post postDB = new DAL.Post()
            {
                PostID = postDTO.PostID,
                Username = postDTO.Username,
                Title = postDTO.Title,
                Content = postDTO.Content,
                ContactName = postDTO.ContactName,
                MobilePhone = postDTO.MobilePhone,
                TelePhone = postDTO.TelePhone,
                ContactEmail = postDTO.Email,
                IsAvailable = true,
                TimeCreated = DateTime.Today,
                PostStatus = 0
            };
            DAL.House houseDB = new DAL.House()
            {

                TypeOfHouseID = postDTO.House.TypeOfHouseID,
                Area = postDTO.House.Area,
                AddressDetail = postDTO.House.AddressDetail,
                Province = postDTO.House.ProvinceID,
                District = postDTO.House.DistrictID,
                //PricePerDay = postDTO.House.PricePerDay,
                //PricePerWeek = postDTO.House.PricePerWeek,
                //PricePerMonth = postDTO.House.PricePerMonth,
                NumSittingRoom = postDTO.House.NumSittingRoom,
                NumBadroom = postDTO.House.NumBadroom,
                Storey = postDTO.House.Storey,
                NumOfWC = postDTO.House.NumOfWC,
                HasGara = postDTO.House.HasGara,
                HasGarden = postDTO.House.HasGarden,
                HasKitchen = postDTO.House.HasKitchen,
                Ordinate = postDTO.House.Ordinate
            };
            //Th1:Nhap ngay,Khong nhap tuan,khong nhap thang
            if (priceperday != 0 && priceperweek == 0 && pricepermonth == 0)
            {
                houseDB.PricePerDay = postDTO.House.PricePerDay;
                houseDB.PricePerWeek = postDTO.House.PricePerDay * 7;
                houseDB.PricePerMonth = postDTO.House.PricePerDay * 30;

            }
            //Th2:Khong nhap ngay,Nhap tuan, ko nhap thang
            if (priceperday == 0 && priceperweek != 0 && pricepermonth == 0)
            {
                houseDB.PricePerDay = postDTO.House.PricePerDay;
                houseDB.PricePerWeek = postDTO.House.PricePerWeek;
                houseDB.PricePerMonth = postDTO.House.PricePerWeek * 4;

            }
            //Th3 Ko nhap ngay,ko nhap tuan,nhap thang
            if (priceperday == 0 && priceperweek == 0 && pricepermonth != 0)
            {
                houseDB.PricePerDay = postDTO.House.PricePerDay;
                houseDB.PricePerWeek = postDTO.House.PricePerWeek;
                houseDB.PricePerMonth = postDTO.House.PricePerMonth;

            }
            //Th4 nhap ngay,nhap tuan,ko nhap thang
            if (priceperday != 0 && priceperweek != 0 && pricepermonth == 0)
            {
                houseDB.PricePerDay = postDTO.House.PricePerDay;
                houseDB.PricePerWeek = postDTO.House.PricePerWeek;
                houseDB.PricePerMonth = postDTO.House.PricePerWeek * 4;

            }
            //Th5 nhap ngay,ko nhap tuan,nhap thang
            if (priceperday != 0 && priceperweek == 0 && pricepermonth != 0)
            {
                houseDB.PricePerDay = postDTO.House.PricePerDay;
                houseDB.PricePerWeek = postDTO.House.PricePerDay * 7;
                houseDB.PricePerMonth = postDTO.House.PricePerMonth;
            }
            //Th6 ko nhap ngay,nhap tuan,nhap thang
            if (priceperday == 0 && priceperweek != 0 && pricepermonth != 0)
            {
                houseDB.PricePerDay = postDTO.House.PricePerDay;
                houseDB.PricePerWeek = postDTO.House.PricePerWeek;
                houseDB.PricePerMonth = postDTO.House.PricePerMonth;
            }
            //Th7 nhap ngay ,nhap tuan ,nhap thang
            if (priceperday != 0 && priceperweek != 0 && pricepermonth != 0)
            {
                houseDB.PricePerDay = postDTO.House.PricePerDay;
                houseDB.PricePerWeek = postDTO.House.PricePerWeek;
                houseDB.PricePerMonth = postDTO.House.PricePerMonth;

            }
            model.UpdatePost(postDB, houseDB);
        }

        public int ChangeAvailable(int postID)
        {
            return model.ChangeAvaliable(postID);
        }

        public void DeletePost(int postId)
        {
            model.DeletePost(postId);
        }
        #endregion //Edit Post

        #endregion

        public List<PostOfUser> GetPostOfUser(string username)
        {
            PostsDataModel model = new PostsDataModel();
            List<PostOfUser> result = new List<PostOfUser>();
            List<Store_Post_GetPostOfUser_Result> l = new List<Store_Post_GetPostOfUser_Result>();
            l = model.GetPostOfUser(username);
            foreach (var item in l)
            {
                PostOfUser p = new PostOfUser();
                p.Available = item.IsAvailable;
                p.PostID = item.PostID;
                p.Status = item.PostStatus;
                p.TimeCreated = item.TimeCreated.ToShortDateString();
                p.Title = item.Title;
                p.Image = Util.imagePath + item.PostAvatar;
                result.Add(p);
            }
            return result;
        }
        public List<Store_Likes_SelectTopPost_Result> GetTopLikePost()
        {
            return model.GetTopLikePost();

        }

        //Get list top Post News
        public List<ResultObject> ListTopPostNew()
        {
            int countrow = 1;
            ManagePostsDataModel model=new ManagePostsDataModel();
            List<Post_ManagePosts_View> listPost = model.GetListPosts(1);
            var query2 = (from item in listPost
                         orderby item.PostID descending 
                         select new
                         {
                             item.PostID,
                             item.Title,
                             item.Province,
                             item.District,
                             item.AvatarPost,
                             item.AvatarPicture,
                             item.PricePerDay,
                             item.PricePerWeek,
                             item.PricePerMonth,
                             item.AddressHouse,
                             item.like_count,
                             item.Username,
                             item.IsAvailable,
                             item.TimeAdded,
                             row = countrow++//add row 

                         }).Take(8);
            var rowresult = query2.ToList();
            List<ResultObject> listresult = new List<ResultObject>();
            foreach (var item in rowresult)
            {
                ResultObject obj=new ResultObject();
                obj.PostID = item.PostID;
                obj.Title = item.Title;
                obj.Province = item.Province;
                obj.District = item.District;
                obj.AvatarPost = item.AvatarPost;
                obj.AvatarUser = item.AvatarPicture;
                obj.PricePerDay = item.PricePerDay;
                obj.AddressHouse = item.AddressHouse;
                 obj.like_count = item.like_count;
                obj.PricePerDay = item.PricePerDay;
                obj.PricePerWeek = item.PricePerWeek;
                obj.PricePerMonth = item.PricePerMonth;
                obj.IsAvailable = item.IsAvailable;
                obj.TimeCreated = item.TimeAdded;
                obj.Row = item.row;
                listresult.Add(obj);
            }
            return listresult;

        }

        public List<PostInProvinceItem> ListNoPostInProvince ()
        {
            PostsDataModel dataModel = new PostsDataModel();
            List<Store_NoHouseInProvince_Result> list = dataModel.GetNoHouseInProvince();
            List<PostInProvinceItem> listResult = new List<PostInProvinceItem>();
            foreach (var i in list)
            {
                PostInProvinceItem item = new PostInProvinceItem();
               // item.RegionId = i.RegionID;
                item.NoPosts = i.NoHouse ?? 0;
                
                listResult.Add(item);
            }
            return listResult;
        }
    }
}